﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Role_Action : BasePage
{
    private int roleId;
    protected List<FreeHouse.Model.TRoleAction> roleActions;
    protected void Page_Load(object sender, EventArgs e)
    {
        UserPower.CheckAction((int)FreeHouse.Power.Actions.TRole.EditAction);
        roleId = Convert.ToInt32(Request.QueryString["roleid"]);
        roleActions = new FreeHouse.BLL.TRoleAction().GetModelList("roleid=" + roleId.ToString());
        if (!IsPostBack)
            BindData();
    }

    protected void BindData()
    {
        actionClass.DataSource = new FreeHouse.BLL.TActionClass().GetAllList();
        actionClass.DataBind();
    }
    protected void actionClass_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //判定ActionClass下的所有Action
            System.Data.DataRowView row = (System.Data.DataRowView)e.Row.DataItem;
            int actionClassId = Convert.ToInt32(row.Row.ItemArray[0]);
            Repeater actions = (Repeater)e.Row.FindControl("actions");
            if (actions != null)
            {
                actions.DataSource = new FreeHouse.BLL.TAction().GetList(0, "ActionClassID="+actionClassId.ToString(), "MethodName");
                actions.DataBind();
                if (actions.Items.Count == 0)
                {
                    Localize nodata =(Localize) e.Row.FindControl("nodata");
                    nodata.Text = "暂无Action";
                }
            }
        }
    }
    protected void save_Click(object sender, EventArgs e)
    {
        FreeHouse.BLL.TRoleAction bllRoleAction = new FreeHouse.BLL.TRoleAction();
        FreeHouse.Model.TRoleAction model = new FreeHouse.Model.TRoleAction();
        model.RoleId = roleId;
        bllRoleAction.Delete(model.RoleId);

        if (Request.Form["action"] == null) return;
        string[] actionIds = Request.Form["action"].Split(',');

        foreach (string s in actionIds)
        {
            model.ActionId = int.Parse(s);
            bllRoleAction.Add(model);
        }
        Response.Write("<script>alert('操作成功');location.href='default.aspx';</script>");
        Response.End();
        //Response.Redirect(Request.UrlReferrer.ToString());
    }

    protected void back_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}
